package com.fifth.home.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Article;
import com.fifth.home.mapper.ArticleMapper;
import com.fifth.home.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 第五组
 * @since 2022-09-08
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    @Resource
    private ArticleMapper articleMapper;

    /**
     * 根据底部文章id查询文章管理信息
     * @param navId
     * @return
     */
    @Override
    public CommonResult selectByNavId(String navId) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("nav_id",navId);
        Article article = articleMapper.selectOne(wrapper);
        if(article!=null) {
            return new CommonResult(2000, "查询文章信息成功", article);
        }else {
            Map<String,String> map = new HashMap<>();
            map.put("navId",navId);
            return new CommonResult(2000, "查询文章信息成功", map);
        }
    }

    /**
     * 修改文章管理信息
     * @param article
     * @return
     */
    @Override
    public CommonResult updateArticle(Article article) {
        //根据navid查询是否有该条记录
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("nav_id",article.getNavId());
        Article article1 = articleMapper.selectOne(wrapper);
        //如果不等于null  执行修改
        if(article1!=null){
            QueryWrapper<Article> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("nav_id",article.getNavId());
            Article article2 = new Article();
            article2.setArtTitle(article.getArtTitle());
            article2.setArtDesc(article.getArtDesc());
            article2.setArtPic(article.getArtPic());
            int update = articleMapper.update(article2,wrapper2);
            return new CommonResult(2000,"修改文章管理信息成功",null);
        }else {
            //否则执行添加
            int insert = articleMapper.insert(article);
            return new CommonResult(2000,"修改文章管理信息成功",null);
        }
    }


}
